package licode.unisop.single.platform.infrastructure.repository.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import licode.unisop.provider.info.SiAssetId;
import licode.unisop.single.platform.application.entity.PlatAssetPropEntity;
import licode.unisop.single.platform.application.gateway.PlatAssetPropDao;
import licode.unisop.single.platform.application.info.PlatAssetPropInfo;
import licode.unisop.single.platform.infrastructure.assembler.UniPlatformSingleAssembler;
import licode.unisop.single.platform.infrastructure.repository.mapper.PlatAssetPropMapper;
import licode.unisop.single.platform.infrastructure.repository.po.PlatAssetPropPO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * uni-platform-single
 *
 * @author licode
 * @date 2016-08-27 15:30:59
 */
@Component
public class PlatAssetPropDaoImpl
        extends ServiceImpl<PlatAssetPropMapper, PlatAssetPropPO>
        implements PlatAssetPropDao {
    @Resource
    private UniPlatformSingleAssembler uniPlatformSingleAssembler;

    @Override
    public void add(PlatAssetPropEntity item) {
        super.save(uniPlatformSingleAssembler.fromPlatAssetPropEntity(item));
    }

    @Override
    public void addBatch(List<PlatAssetPropEntity> items) {
        super.saveBatch(items.stream().map(item -> uniPlatformSingleAssembler.fromPlatAssetPropEntity(item))
                .collect(Collectors.toList()));
    }

    @Override
    public void update(PlatAssetPropEntity item) {
        super.updateById(uniPlatformSingleAssembler.fromPlatAssetPropEntity(item));
    }

    @Override
    public void updateBatch(List<PlatAssetPropEntity> items) {

    }

    @Override
    public void removeWithId(Serializable id) {
        super.removeById(id);
    }

    @Override
    public void remove(PlatAssetPropInfo info) {
        super.remove(Wrappers.<PlatAssetPropPO>lambdaUpdate()
                .eq(PlatAssetPropPO::getPlatformId, info.getPlatformId())
                .eq(PlatAssetPropPO::getAssetId, info.getAssetId())
                .eq(PlatAssetPropPO::getAssetType, info.getAssetType()));
    }

    @Override
    public PlatAssetPropEntity getWithId(Serializable id) {
        return uniPlatformSingleAssembler.fromPlatAssetProp(super.getById(id));
    }

    @Override
    public List<PlatAssetPropEntity> getAssetProps(SiAssetId info) {
        List<PlatAssetPropPO> props = super.lambdaQuery()
                .eq(PlatAssetPropPO::getPlatformId, info.getOwnerId())
                .eq(PlatAssetPropPO::getAssetId, info.getAssetId())
                .eq(PlatAssetPropPO::getAssetType, info.getAssetType())
                .list();
        if (null != props && !props.isEmpty()) {
            return props.stream().map(item -> uniPlatformSingleAssembler.fromPlatAssetProp(item))
                    .collect(Collectors.toList());
        }
        return null;
    }
}
